package cn.yl.provider.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/provider")
public class ProviderController {

//    private final RestTemplate restTemplate;
//
//    private final DiscoveryClient discoveryClient;
//
//    public ProviderController(DiscoveryClient discoveryClient, RestTemplate restTemplate) {
//        this.discoveryClient = discoveryClient;
//        this.restTemplate = restTemplate;
//    }

    @Value("${server.port}")
    private String port;

//    @GetMapping("/port")
//    public String providerPort(){
////        return restTemplate.getForObject("http://localhost:8001/consumer/port",String.class);
//        ServiceInstance consumer = discoveryClient.getInstances("consumer").get(0);
//        System.out.println("consumer = " + consumer.getUri());
//        String url= consumer.getUri() + "/consumer/port";
//        return restTemplate.getForObject(url,String.class);
//    }


    @GetMapping("/port")
    public String getPort() {
        return "来自provider的端口号 ： "+port;
    }
    @GetMapping("/feign/timeout")
    public String providerFeignTimeOut() {
        try {
            // 休眠3秒
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return port;
    }
}
